package MiGong;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class GenerateMaze {
	
	private static final int SIZE = 8;
    private static final Random r = new Random();
    private static final Scanner scanner = new Scanner(System.in);
    
    //设置特殊格个数:障碍、怪物、药水、装备
    private static double[] weights = {0.4,0.2,0.3,0.3};
    private static int totalSpecial = 28; 
    
    
    public static int[][] generateMaze() {
    	int[][] maze = new int[SIZE][SIZE];
        // 初始化迷宫
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                maze[i][j] = 0;
            }
//            System.out.println(Arrays.toString(maze[i]));
        }
        return maze;
    }
	
	//权重设置
	private static int getIndex(double[] weights){
		double[] cumulativeWeights = new double[weights.length];
		double sum = 0;
		for(int i=0; i<weights.length; i++){
			sum += weights[i];
			cumulativeWeights[i] = sum;
		}
		//随机生成
		double randomValue = r.nextDouble()*sum;
		for(int j=1; j<=cumulativeWeights.length; j++){
			if(randomValue<cumulativeWeights[j-1]){
				return j;
			}
		}
		return 0;
	}
	
	//设置数组
	public static void setSpecial(int[][] maze){
		for(int i=0; i<totalSpecial; i++){
			int row = r.nextInt(SIZE);
			int col = r.nextInt(SIZE);
			while(row==0 && col==0 || row==SIZE-1 && col==SIZE-1){
				row = r.nextInt(SIZE);
				col = r.nextInt(SIZE);
			}
			switch(getIndex(weights)){
			case 1:
				maze[row][col] = 1;  //障碍0.4
				break;
			case 2:
				maze[row][col] = 2; //怪物0.2
				break;
			case 3:
				maze[row][col] = 3; //药水0.3
				break;
			case 4:
				maze[row][col] = 4; //装备0.3
				break;
			}
		}
	}
	
	//打印数组
	public static void printMaze(int[][] maze, int px, int py){
		for(int i=0; i<SIZE; i++){
			for(int j=0; j<SIZE; j++){
				if (i == px && j == py) {
                    System.out.print("P ");
                }
				else{
					switch(maze[i][j]){
					case 0:
						System.out.print("□ ");
						break;
					case 1:
	                    System.out.print("■ ");
	                    break;
					case 2:
						System.out.print("! ");
						break;
					case 3:
						System.out.print("+ ");
						break;
					case 4:
						System.out.print("E ");
						break;
	                }
				}
				
			}
			System.out.println();
		}
	}
	
}
